package main

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func main() {

	ch := make(chan string)
	go sender(ch)
	go recver(ch)
	time.Sleep(1e9)

	for i := 0; i < 12; i++ {
		goo(i)
	}

	count := make(chan int)
	wg.Add(2)
	fmt.Println("Start Goroutines")

	go printCounts("Goroutine-1", count)
	go printCounts("Goroutine-2", count)
	fmt.Println("Communication of channel begins")
	count <- 1
	fmt.Println("Wating To Finish")
	wg.Wait()
	fmt.Println("\nTerminating the Program")

}

func printCounts(label string, count chan int) {
	defer wg.Done()
	for {
		val, ok := <-count
		if !ok {
			fmt.Println("Channel was closed:", label)
			return
		}
		fmt.Printf("Count: %d received from %s \n", val, label)
		if val == 10 {
			fmt.Printf("Channel Closed from %s \n", label)
			// Close the channel
			close(count)
			return
		}
		val++
		count <- val
	}

}
func goo(s int) {
	counter := make(chan int, 1)
	go func() {
		counter <- s
	}()
	fmt.Println(<-counter)
}

func sender(ch chan string) {
	ch <- "malongshuai"
	ch <- "goxiaofang"
	ch <- "wugi"
	ch <- "tuner"
}
func recver(ch chan string) {
	var recv string
	for {
		recv = <-ch
		fmt.Println(recv)
	}
}
